package com.yeyuanqi.hottop.controller;

import com.yeyuanqi.hottop.common.Result;
import com.yeyuanqi.hottop.service.*;
import com.yeyuanqi.hottop.untils.BasicLineChartDataUtil;
import com.yeyuanqi.hottop.untils.StackedLineChartDataUtil;
import com.yeyuanqi.hottop.vo.BasicLineAndBarVO;
import com.yeyuanqi.hottop.vo.HotListVO;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * 折线图控制器
 * @author yeyuanqi
 * @since 2021-12-22
 */
@RestController
@RequestMapping("/lineandbarchart")
public class LineAndBarChartController {

    @Autowired
    private RedisService redisService;

    @Autowired
    private StackedLineChartDataUtil stackedLineChartDataUtil;

    @Autowired
    private BasicLineChartDataUtil basicLineChartDataUtil;

    @Autowired
    private TBaiduhotService tBaiduhotService;

    @Autowired
    private TJinrihotService tJinrihotService;

    @Autowired
    private TTxhotService tTxhotService;

    @Autowired
    private TWeibohotService tWeibohotService;

    /**
     *  最新热搜数据
     *  @return 折线图和柱状图数据
     */
    @GetMapping("/{topnewhot}")
    public Result selectLineChartForNewHot(@PathVariable("topnewhot") String topnewhot){
        switch (topnewhot){
            case "bdnewhot":
                //查询缓存，有缓存直接返回
                if (redisService.lSize("bdLineAndBarData")!=0){
                    List<Object>  bdtopnewshotList= redisService.lRange("bdLineAndBarData", 0, -1);
                    return Result.succ(bdtopnewshotList);
                }
                List<HotListVO> bdhotListVOS = tBaiduhotService.selectNewHot();
                BasicLineAndBarVO bdtopnewshotList = basicLineChartDataUtil.getBasicLineAndBarChartHotData(bdhotListVOS);
                List<BasicLineAndBarVO> bdhotList = new ArrayList<>();
                bdhotList.add(bdtopnewshotList);
                return Result.succ(bdhotList);
            case "jrnewhot":
                if (redisService.lSize("jrLineAndBarData")!=0){
                    List<Object>  jrtopnewshotList= redisService.lRange("jrLineAndBarData", 0, -1);
                    return Result.succ(jrtopnewshotList);
                }
                List<HotListVO> jrhotListVOS = tJinrihotService.selectNewHot();
                BasicLineAndBarVO jrtopnewshotList = basicLineChartDataUtil.getBasicLineAndBarChartHotData(jrhotListVOS);
                List<BasicLineAndBarVO> jrhotList = new ArrayList<>();
                jrhotList.add(jrtopnewshotList);
                return Result.succ(jrhotList);
            case "txnewhot":
                if (redisService.lSize("txLineAndBarData")!=0){
                    List<Object>  txtopnewshotList= redisService.lRange("txLineAndBarData", 0, -1);
                    return Result.succ(txtopnewshotList);
                }
                List<HotListVO> txhotListVOS = tTxhotService.selectNewHot();
                BasicLineAndBarVO txtopnewshotList = basicLineChartDataUtil.getBasicLineAndBarChartHotData(txhotListVOS);
                List<BasicLineAndBarVO> txhotList = new ArrayList<>();
                txhotList.add(txtopnewshotList);
                return Result.succ(txhotList);
            case "wbnewhot":
                if (redisService.lSize("wbLineAndBarData")!=0){
                    List<Object>  wbtopnewshotList= redisService.lRange("wbLineAndBarData", 0, -1);
                    return Result.succ(wbtopnewshotList);
                }
                List<HotListVO> wbhotListVOS = tWeibohotService.selectNewHot();
                BasicLineAndBarVO wbtopnewshotList = basicLineChartDataUtil.getBasicLineAndBarChartHotData(wbhotListVOS);
                List<BasicLineAndBarVO> wbhotList = new ArrayList<>();
                wbhotList.add(wbtopnewshotList);
                return Result.succ(wbhotList);
            default:
                return Result.fail("没有该网站的热搜信息");
        }
    }

    /**
     *  所有平台近七天top1数据
     *  @return 折线图堆叠数据
     */

    @GetMapping("/line/alltoponeforsevendays")
    public Result getStackedLineChartData(){
        return Result.succ(stackedLineChartDataUtil.getTopOneDataForSevenDays());
    }

    /**
     *  历史热搜数据
     *  @return 折线图和柱状图数据 list
     */
    @GetMapping("/{sitename}/{day}/{min}")
    public Result selectLineChartForNewHotByTime(@PathVariable("sitename") String sitename , @PathVariable("day") String day, @PathVariable("min") String min){
        if ("all".equals(min)) {
            min = "%";
        }else if (!"".equals(min) && null != min){
            min = min + "%";
        }
        switch (sitename){
            case "bdhot":
                List<HotListVO> bdhotListVOS = tBaiduhotService.selectHotByTime(day, min);
                if (bdhotListVOS.size() != 0){
                    BasicLineAndBarVO bdnewshotList = basicLineChartDataUtil.getBasicLineAndBarChartHotData(bdhotListVOS);
                    //需要返回列表格式
                    List<BasicLineAndBarVO> hotList = new ArrayList<>();
                    hotList.add(bdnewshotList);
                    return Result.succ(hotList);
                } else {
                    return Result.fail("没有该时间的热搜");
                }
            case "jrhot":
                List<HotListVO> jrhotListVOS = tJinrihotService.selectHotByTime(day, min);
                if (jrhotListVOS.size() != 0){
                    BasicLineAndBarVO jrnewshotList = basicLineChartDataUtil.getBasicLineAndBarChartHotData(jrhotListVOS);
                    List<BasicLineAndBarVO> hotList = new ArrayList<>();
                    hotList.add(jrnewshotList);
                    return Result.succ(hotList);
                } else {
                    return Result.fail("没有该时间的热搜");
                }
            case "txhot":
                List<HotListVO> txhotListVOS = tTxhotService.selectHotByTime(day, min);
                if (txhotListVOS.size() != 0){
                    BasicLineAndBarVO txnewshotList = basicLineChartDataUtil.getBasicLineAndBarChartHotData(txhotListVOS);
                    List<BasicLineAndBarVO> hotList = new ArrayList<>();
                    hotList.add(txnewshotList);
                    return Result.succ(hotList);
                } else {
                    return Result.fail("没有该时间的热搜");
                }
            case "wbhot":
                List<HotListVO> wbhotListVOS = tWeibohotService.selectHotByTime(day, min);
                if (wbhotListVOS.size() != 0){
                    BasicLineAndBarVO wbnewshotList = basicLineChartDataUtil.getBasicLineAndBarChartHotData(wbhotListVOS);
                    List<BasicLineAndBarVO> hotList = new ArrayList<>();
                    hotList.add(wbnewshotList);
                    return Result.succ(hotList);
                } else {
                    return Result.fail("没有该时间的热搜");
                }
            default:
                return Result.fail("没有该网站的热搜信息");
        }
    }

}
